package com.hmall.controller;

import com.hdmall.business.common.annotation.LoginRequired;
import com.hdmall.business.common.common.ServerResponse;
import com.hmall.vo.BigList;
import com.hmall.model.SysUser;
import com.hmall.service.ProductClassService;
import com.hmall.util.TokenUtil;
import com.hmall.vo.PageVo;
import com.hmall.vo.ProductClassDetailVo;
import com.hmall.vo.ShortCompete;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author zhoutao
 */
@RestController
@RequestMapping("/class/")
@CrossOrigin(origins = "http://localhost:8088", maxAge = 3600)
@Api(value = "产品分类API", description = "产品列表接口")
public class ProductClassController {

    private final ProductClassService iProductClassService;

    private final JedisPool jedisPool;

    TokenUtil tokenUtil = new TokenUtil();

    @Autowired
    public ProductClassController(ProductClassService iProductClassService, JedisPool jedisPool) {
        this.iProductClassService = iProductClassService;
        this.jedisPool = jedisPool;
    }

    /**
     * @Date 2018/11/9 11:01
     * @Param [token, typeName, shopId]
     * @return com.hmall.common.ServerResponse<com.hmall.vo.BigList>
     * @Description // 根据返回给的点击操作所传过来的参数给一个结果集（包含了所有的查询的单个属性）
     */
    @PostMapping("filter")
    @ApiOperation(value = "筛选列表")
    public ServerResponse<BigList> listBigList(HttpServletRequest request, String typeName, @RequestParam(value = "shopId", defaultValue = "1") Integer shopId,
                                               @RequestParam(value = "type", defaultValue = "1") Integer type) {

        return iProductClassService.listBigList(request, "".equals(typeName) ? null : typeName, shopId, type);

    }

    /**
     * @Date 2018/11/9 11:02
     * @Param [token, typeName, fineAndPoreName, glossName, gradeName, singleShareName, sectionName, colorName, twistName, networkName, shopId, pageNum, pageSize]
     * @return com.hmall.common.ServerResponse<com.hmall.vo.PageVo<com.hmall.vo.ShortCompete>>
     * @Description // 未登录不能看到任何价格  普通登录能看到标准价格  资质通过后登录能看到所有价格
     */
    @PostMapping("Compete")
    @ApiOperation(value = "具体产品", notes = "typeName: 产品类型|fineAndPoreName:纤度孔数|glossName:光泽|gradeName:等级|singleShareName:单合股|sectionName:截面|colorName:颜色|twistName:捻向|networkName:网络|factory:工厂 2018/10/22")
    public ServerResponse<PageVo<ShortCompete>> listComProduct(String token, String typeName, String fineAndPoreName, String glossName, String gradeName, String singleShareName,
                                                               String sectionName, String colorName, String twistName, String networkName,
                                                               @RequestParam(value = "shopId", defaultValue = "1")int shopId,
                                                               @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                                               @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                               @RequestParam(value = "type", defaultValue = "1") Integer type) throws Exception {

        SysUser user = null;
        if(token != null && !"".equals(token)) {
            ServerResponse response = tokenUtil.tokenToUser(jedisPool, token);
            if(response.getStatus() == 0){
                 user =  (SysUser) response.getData();
            }
        }
        return iProductClassService.listComProduct(user, typeName, fineAndPoreName, glossName, gradeName,singleShareName, sectionName, colorName, twistName, networkName, shopId, pageNum, pageSize, type);
    }


    /**
     * @Date 2018/11/9 11:06
     * @Param [productId, token]
     * @return com.hmall.common.ServerResponse<com.hmall.vo.ProductClassDetailVo>
     * @Description // 产品详情现在必须登录之后才能访问
     */
    @LoginRequired
    @GetMapping("detail")
    @ApiOperation(value = "产品详情")
    public ServerResponse<ProductClassDetailVo> listProductDetail(Integer productId, String token) throws Exception {

        
        SysUser user = tokenUtil.tokenToUser(jedisPool, token).getData();
        //如果是有绑定用户的访问，要记录下浏览记录
        if(!"".equals(token) && token != null){
            iProductClassService.recordView(user.getUserId(), productId);
        }
        return iProductClassService.listProductDetail(user.getUserId(), productId);
    }


    @GetMapping("detailContent")
    @ApiOperation(value = "图文介绍")
    public ServerResponse<String> listProductContent(Integer productId)  {

       return iProductClassService.listProductContent(productId);
    }



}




